﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ProxyTool.Models;
using ProxyTool.Properties;

namespace ProxyTool.Controls
{
    public delegate bool TabPageClosing(TabPage tabPage);

    public class ProxyTabControl : TabControl
    {
        private readonly Image _icon;
        private readonly int _iconWorH = 16;
        public TabPageClosing OnTabPageClosing;

        public ProxyTabControl()
        {
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            _icon = Resources.close;
            _iconWorH = _icon.Width;
            _iconWorH = _icon.Height;
        }

        /// <summary>
        ///     添加tabPage
        /// </summary>
        /// <param name="tabPageName"></param>
        /// <param name="proxyData"></param>
        public TabPage AddProxyTabPage(string tabPageName, ProxyData proxyData)
        {
            var proxyEditConrol = new ProxyEditControl(proxyData);
            proxyEditConrol.Dock = DockStyle.Fill;
            var tabPage = new TabPage
            {
                Name = tabPageName + Guid.NewGuid(),
                Text = tabPageName + " ",
                ToolTipText = tabPageName,
                Tag = proxyEditConrol
            };
            tabPage.Controls.Add(proxyEditConrol);
            this.Controls.Add(tabPage);
            this.SelectedTab = tabPage;
            return tabPage;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            var g = e.Graphics;
            var r = GetTabRect(e.Index);
            if (e.Index == this.SelectedIndex) //当前选中的Tab页，设置不同的样式以示选中
            {
                var selected_color = Brushes.LightBlue; //选中的项的背景色
                g.FillRectangle(selected_color, r); //改变选项卡标签的背景色
                var title = this.TabPages[e.Index].Text;
                g.DrawString(title, this.Font, new SolidBrush(Color.Black), new PointF(r.X, r.Y + 5)); //PointF选项卡标题的位置
                r.Offset(r.Width - _iconWorH, 2);
                g.DrawImage(_icon, new Point(r.X, r.Y));
                //选项卡上的图标的位置 fntTab = new System.Drawing.Font(e.Font, FontStyle.Bold);
            }
            else //非选中的
            {
                var selected_color = Brushes.AliceBlue; //选中的项的背景色
                g.FillRectangle(selected_color, r); //改变选项卡标签的背景色
                var title = this.TabPages[e.Index].Text + "  ";
                g.DrawString(title, this.Font, new SolidBrush(Color.Black), new PointF(r.X, r.Y + 5)); //PointF选项卡标题的位置
                r.Offset(r.Width - _iconWorH, 2);
                g.DrawImage(_icon, new Point(r.X, r.Y)); //选项卡上的图标的位置
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            var point = e.Location;
            var r = GetTabRect(this.SelectedIndex);
            r.Offset(r.Width - _iconWorH - 3, 2);
            r.Width = _iconWorH;
            r.Height = _iconWorH;
            if (r.Contains(point))
            {
                if (this.SelectedIndex != 0)
                {
                    this.CloseTabPage(this.SelectedTab);
                    //this.TabPages.RemoveAt(this.SelectedIndex);
                }
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ProxyTabControl
            // 
            this.ResumeLayout(false);

        }

        /// <summary>
        /// 关闭页签
        /// </summary>
        /// <param name="tabPage"></param>
        public void CloseTabPage(TabPage tabPage)
        {
            if (tabPage == null) return;
            bool isCanClose = true;
            if (OnTabPageClosing != null)
                isCanClose = OnTabPageClosing.Invoke(tabPage);
            if (!isCanClose) return;
            int currentIndex = tabPage.TabIndex;
            TabPage selectTabPage = null;
            foreach (TabPage tabPage2 in this.TabPages)
            {
                if (tabPage2.TabIndex == currentIndex) continue;
                if (selectTabPage == null)
                {
                    selectTabPage = tabPage2;
                    continue;
                }
                if (tabPage2.TabIndex < currentIndex && tabPage2.TabIndex > selectTabPage.TabIndex)
                    selectTabPage = tabPage2;
            }
            if (selectTabPage!=null)
                this.SelectedTab = selectTabPage;
            this.TabPages.Remove(tabPage);
        }

        public ProxyEditControl GetCurrentProxyEditConrol()
        {
            var tabPage = this.SelectedTab;
            return GetProxyEditConrol(tabPage);
        }

        public static ProxyEditControl GetProxyEditConrol(TabPage tablPage)
        {
            if (tablPage == null || tablPage.Tag == null) return null;
            return tablPage.Tag as ProxyEditControl;
        }
    }
}